<?php
/**
 * PHP Class
 *
 * LICENSE
 *
 * Processing: Module helper
 *
 * @category   	Joomzf Cms
 * @package    	Module Helper
 * @copyright  	Copyright vinanewstar.vn@gmail.com
 * @author		GiangNam (vinanewstar.vn@gmail.com)
 * @license    	GNU General Public License Version 2 or later (the "GPL")
 * @version     v1.0 May 27, 2012 - 3:06:36 PM
 */
class Joomzf_Modules_Helper{
	
	public static $listModule;
	public $action;
	public $view;
	public $request;
	public $template;
	
	private $_xhtml;
	private $_positionFromTemplate;
	
	private $_moduleList;
	private $_options;
	private $_comUrl;
	private $_area;
	
	
	public function __construct($xhtml,Zend_View $view){
		
		$this->_xhtml = $xhtml;
		$this->view   		= $view;
		$this->request 		= $this->view->request;
		$this->template 	= $this->view->template;
		
		$this->_comUrl		= Zend_Registry::get("comUrl");
		$this->_area		= $this->template->area;
		
		$this->loadTemplateModulePos();
		$this->loadModuleList();
		self::$listModule = $this->_moduleList;
		/**
		 * return list to use in translate
		 */
		
	}
	
	/**
	 * load module position in template
	 */
	public function loadTemplateModulePos(){
		$pos = array();
		$content = $this->_xhtml;
		while(strpos($content,'{include {"type":"modules"')!=false){
			$i1 	= strpos($content,'{include ');
			$i2 	= strpos($content,' include}');
			$pos[] 	= substr($content,$i1,$i2-$i1+9);
			$content 	= substr($content,$i2+9);
		}
		if(count($pos) >0)foreach ($pos as $flagStr){
			$end = strpos($flagStr, 'include}');
			$options[$flagStr] = json_decode(trim(substr($flagStr, 8,$end-8)));
		}
		$this->_positionFromTemplate = $options;
	}
	/**
	 * load module (that active) list from database
	 */
	public function loadModuleList(){
		// position conditions
		foreach ($this->_positionFromTemplate as $options) {
			$pos[]= '"'.$options->name.'"';
		}
		$pos = implode(",",$pos);
		//menu assignment condition, published condition, position condition
		$db = Zend_Registry::get("DBO");
		$sql2 = $db->select()
			->from(TBL_PRF."menu as m",array())
			->where("m.link_option   = ?",$this->_comUrl->option)
			->where("m.link_control  = ?",$this->_comUrl->control)
			->where("m.link_activity = ?",$this->_comUrl->activity)
			->joinLeft(TBL_PRF."module_menu as mm","m.id = mm.menu_id" ,array("mm.module_id"));
		$ids = implode(",", $db->fetchAll($sql2,array(),Zend_Db::FETCH_COLUMN));
		$sql = $db ->select()
			->from(TBL_PRF.'modules',array('id'))
			->where('published = 1')
			->order('order ASC')
			->where('area = ?',$this->_area)
			->where('published = 1')
			->where('position IN('.$pos.')')
			->where('id IN ('.$ids.')');
		
		
		$module_ids = $db->fetchAll($sql,array(),Zend_Db::FETCH_COLUMN);
		foreach ($module_ids as $id){
			$this->_moduleList[] = new Joomzf_Modules_Module($id);
		}
		
	}
	/**
	 * render module
	 * @return Ambigous <unknown, mixed>
	 */
	public function render(){
		foreach($this->_positionFromTemplate AS $search => $options) {
			if(count($this->_moduleList)>0)foreach($this->_moduleList AS $module){
				if($module->position == $options->name){
					if($module->params->cache == 1){
						if($module->params->cache_time == 0) $cacheTime = null; 
						else $cacheTime = $module->params->cache_time;
						$cache = Joomzf_Cache::initModule($cacheTime);
						
						$modContent .= Joomzf_Cache::getVariableFromCache($cache,$module->name,$this,'obRecord',
														array('style'=>$style,'module'=>$module));
					}else $modContent .= $this->obRecord(array('style'=>$style,'module'=>$module));
				}
				
			}
			$this->_xhtml = str_replace($search,$modContent,$this->_xhtml);
			$modContent = "";
		}
		return $this->_xhtml;
	}
	/**
	 * require and record the file
	 * @params: array $option
	 * @return string
	 */
	public function obRecord($option){
		$style = $option['style'];
		$module = $option['module'];
		ob_start();
		require(Joomzf_Modules_Helper::getStylePath($style, $this));
		$modContent .= ob_get_contents();
		ob_clean();
		
		return $modContent;
	}
	/**
	 * get style path
	 * @param string $style
	 * @param Joomzf_Modules_Helper $module
	 * @return string
	 */
	public static function getStylePath($style, Joomzf_Modules_Helper $module){
		if ($style == null) $style ="none";
		$defaultStyle = array("horz","none","outline","rounded","table","xhtml");
		if (array_search($style, $defaultStyle)){
			return BASE_PATH.DS."library".DS."Joomzf".DS."Modules".DS."Style".DS.$style.".php";
		}
		return $module->template->path.DS."Modstyle".DS.$style.".php";
	}
	/**
	 * get area
	 * return 0 or 1
	 */
	public function getArea(){
		return $this->_area;
	}
	/**
	 *
	 * @param Joomzf_Modules_Helper $module
	 * @param stdClass $mod
	 * @return Path to layout. (template or module)
	 */
	public static function getLayout(Joomzf_Modules_Helper $helper, Joomzf_Modules_Module $mod){
		$templatePath = $helper->template->path;
		$tpath 		  = $templatePath.DS.'html'.DS.$mod->name.DS.'default.php';
		$mpath 		  = ($helper->getArea() == 0) ? FRONTEND_MODULES_PATH.DS.$mod->name.DS.'tmpl'.DS.'default.php'
		: BACKEND_MODULES_PATH.DS.$mod->name.DS.'tmpl'.DS.'default.php';
		if (file_exists($tpath)) {
			return $tpath;
		}
		return $mpath;
	}
	/**
	 * 
	 * @param Joomzf_Modules_Helper $helper
	 * @param Joomzf_Modules_Module $mod
	 * @return string
	 */
	public static function getPath(Joomzf_Modules_Helper $helper, Joomzf_Modules_Module $mod){
		return ($helper->getArea() == 0) ? FRONTEND_MODULES_PATH.DS.$mod->name.DS.'index.php'
		: BACKEND_MODULES_PATH.DS.$mod->name.DS.'index.php';
	}
	/**
	 *
	 * @param Joomzf_Modules_Helper $helper
	 * @param Joomzf_Modules_Module $mod
	 * @return string: url to module
	 */
	public static function getUrl(Joomzf_Modules_Helper $helper, Joomzf_Modules_Module $mod){
		return ($helper->getArea() == 0) ? FRONTEND_MODULES_URL.DS.$mod->name
		: BACKEND_MODULES_URL.DS.$mod->name;
	}
	public function getLink(){
		return $this->_comUrl;
	}
}